﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repositories
{

    public class AchievementRepository
    {
        gsDBDataContext link = new gsDBDataContext();

        /// <summary>
        /// Gets Achievement based on achievementID
        /// Precondition: achievementID must exist
        /// Postcondition: Achievement has been returned
        /// Author: Bjarmi mod. by Alma
        /// Date: 07.05.2012
        /// </summary>
        /// <param name="accountID">ID of account to get.</param>
        public Achievement getAchievementByID(int achieveID)
        {
            Achievement selectedAchievement = (from a in link.Achievements
                                               where a.id == achieveID
                                               select a).First();            
            return selectedAchievement; 
        }

        /// <summary>
        /// Gets user Achievements
        /// Precondition: user must exist
        /// Postcondition: A list of achievements has been returned
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 08.05.2012
        /// </summary>
        /// <param name="accountEmail">E-mail address of account</param>
        public IQueryable<Achievement> getAchievementByUser(string myUserID)
        {
            IQueryable<Achievement> userAchievements = (from a in link.Achievements
                                                        from u in link.UserAchievements
                                                        where u.achievementID == a.id && u.userID == myUserID
                                                        select a);
            return userAchievements;
        }

        /// <summary>
        /// Returns a queryable list of all achievements of the course with the specified ID
        /// Precondition: course must exist
        /// Postcondition: All achievements of the course have been returned
        /// Author: Aron Mar mod. by Alma
        /// Date: 8.5.12
        /// </summary>
        /// <param name="courseID">The ID of the course</param>
        public IQueryable<Achievement> getAssignmentsOfCourse(int courseID)
        {
            IQueryable<Achievement> achieves = (from a in link.Achievements
                                              from cl in link.CourseLevels
                                              where cl.cID == courseID && a.id == cl.lvlID
                                              select a);
            return achieves;
        }

        /// <summary>
        /// Returns a queryable list of all achievements of the level with the specified ID
        /// Precondition: level must exist
        /// Postcondition: All assignments of the level have been returned
        /// Author: Aron Mar mod. by Alma
        /// Date: 8.5.12
        /// </summary>
        /// <param name="courseID">The ID of the course</param>
        public IQueryable<Achievement> getAchievementsOfLevel(int levelID)
        {
            IQueryable<Achievement> achieves = (from a in link.Achievements
                                              where a.id == levelID
                                              select a);
            return achieves;
        }
        /// <summary>
        /// Returns a list of all Achievements
        /// Precondition:
        /// Postcondition: List of achievements has been returned
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 08.05.2012
        /// </summary>
        public IQueryable<Achievement> getAllAchievements()
        {
            return link.Achievements;
        }

        /// <summary>
        /// Adds an achievement to the database.
        /// Precondition: 
        /// Postcondition: Achievement has been added to the database
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 03.05.2012
        /// </summary>
        /// <param name="account">The account to add.</param>
        public bool addAchievement(Achievement myAchievement)
        {
            try
            {
                link.Achievements.InsertOnSubmit(myAchievement);
            }
            catch (Exception)
            {
                return false;
            }
            return true; 
        }

        /// <summary>
        /// Edits achievement information for a specific account.
        /// Precondition: User must be an administrator and the account must exist
        /// Postcondition: Achievement information has been updated in the database
        /// Author: Aron Mar mod. by Alma
        /// Date: 8.5.2012
        /// </summary>
        public bool editAchievement(Achievement myAchievement)
        {
            try
            {
                var achieve = (from u in link.Achievements
                            where u.id == myAchievement.id
                            select u).First();

                achieve.name = myAchievement.name;
                achieve.imgPath = myAchievement.imgPath;
                achieve.pointValue = myAchievement.pointValue;
                achieve.descript = myAchievement.descript;

                save();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        /// <summary>
        /// Saves changes to database
        /// Precondition:
        /// Postcondition: Database has been updated
        /// Author: Aron og Bjarmi
        /// Date: 03.05.2012
        /// </summary>
        public void save()
        {
            link.SubmitChanges();
        }
    }
}